
<template>
  <div class="home">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="container">
        <h4 class="page-title">前端开发技术课程</h4>
        <p class="page-subtitle">掌握现代前端开发技术，构建优秀的用户界面</p>
      </div>
    </div>

    <!-- 知识图谱区域 -->
    <div class="knowledge-map-section">
      <div class="container">
        <div class="section-header">
          <!-- <h2>课程知识图谱</h2> -->
          <p>点击节点查看相关内容，拖拽节点重新布局</p>
        </div>
        
        <div class="knowledge-map">
          <!-- 加载状态 -->
          <div v-if="isLoading" class="loading-container">
            <el-icon class="loading-icon" :size="48">
              <Loading />
            </el-icon>
            <p>正在加载知识图谱...</p>
          </div>
          
          <!-- 知识图谱 -->
          <VueFlow 
            v-else
            v-model:nodes="flowNodes" 
            v-model:edges="flowEdges"
            :default-viewport="{ zoom: 0.8 }"
            :min-zoom="0.2"
            :max-zoom="2"
            :snap-to-grid="true"
            :snap-grid="[20, 20]"
            @node-click="handleNodeClick"
            class="vue-flow"
          >
            <template #node-knowledge="{ data }">
              <div class="knowledge-node" :class="data.level">
                <div class="node-content">
                  <div class="node-icon">
                    <el-icon :size="24">
                      <component :is="data.icon" />
                    </el-icon>
                  </div>
                  <div class="node-title">{{ data.title }}</div>
                  <div class="node-subtitle">{{ data.subtitle }}</div>
                </div>
              </div>
            </template>
            
            <Background :size="2" :gap="20" pattern-color="#BDBDBD" />
            <Controls />
            <MiniMap />
          </VueFlow>
        </div>
      </div>
    </div>

    <!-- 课程统计 -->
    <div class="stats-section">
      <div class="container">
        <el-row :gutter="20">
          <el-col :xs="12" :sm="6">
            <div class="stat-card">
              <div class="stat-icon">
                <el-icon :size="16" color="#409eff">
                  <Document />
                </el-icon>
              </div>
              <div class="stat-content">
                <div class="stat-number">24</div>
                <div class="stat-label">讲义课程</div>
              </div>
            </div>
          </el-col>
          <el-col :xs="12" :sm="6">
            <div class="stat-card">
              <div class="stat-icon">
                <el-icon :size="16" color="#67c23a">
                  <Cpu />
                </el-icon>
              </div>
              <div class="stat-content">
                <div class="stat-number">12</div>
                <div class="stat-label">实验项目</div>
              </div>
            </div>
          </el-col>
          <el-col :xs="12" :sm="6">
            <div class="stat-card">
              <div class="stat-icon">
                <el-icon :size="16" color="#e6a23c">
                  <VideoPlay />
                </el-icon>
              </div>
              <div class="stat-content">
                <div class="stat-number">36</div>
                <div class="stat-label">教学视频</div>
              </div>
            </div>
          </el-col>
          <el-col :xs="12" :sm="6">
            <div class="stat-card">
              <div class="stat-icon">
                <el-icon :size="16" color="#f56c6c">
                  <User />
                </el-icon>
              </div>
              <div class="stat-content">
                <div class="stat-number">156</div>
                <div class="stat-label">学习人数</div>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>

    <!-- 知识点详情对话框 -->
    <el-dialog 
      v-model="showNodeDetail" 
      :title="selectedNode?.title" 
      width="800px"
      class="node-detail-dialog"
    >
      <div v-if="selectedNode" class="node-detail">
        <div class="detail-header">
          <div class="detail-icon">
            <el-icon :size="32">
              <component :is="selectedNode.icon" />
            </el-icon>
          </div>
          <div class="detail-info">
            <h3>{{ selectedNode.title }}</h3>
            <p>{{ selectedNode.description }}</p>
          </div>
        </div>
        
        <el-tabs v-model="activeTab">
          <el-tab-pane label="相关讲义" name="lectures">
            <div class="content-list">
              <div 
                v-for="lecture in selectedNode.lectures" 
                :key="lecture.id"
                class="content-item"
                @click="goToLecture(lecture.id)"
              >
                <el-icon><Document /></el-icon>
                <span>{{ lecture.title }}</span>
                <el-tag size="small">第{{ lecture.chapter }}章</el-tag>
              </div>
            </div>
          </el-tab-pane>
          
          <el-tab-pane label="相关视频" name="videos">
            <div class="content-list">
              <div 
                v-for="video in selectedNode.videos" 
                :key="video.id"
                class="content-item"
                @click="goToVideo(video.id)"
              >
                <el-icon><VideoPlay /></el-icon>
                <span>{{ video.title }}</span>
                <el-tag size="small" type="success">{{ video.duration }}</el-tag>
              </div>
            </div>
          </el-tab-pane>
          
          <el-tab-pane label="相关实验" name="experiments">
            <div class="content-list">
              <div 
                v-for="experiment in selectedNode.experiments" 
                :key="experiment.id"
                class="content-item"
                @click="goToExperiment(experiment.id)"
              >
                <el-icon><Cpu /></el-icon>
                <span>{{ experiment.title }}</span>
                <el-tag size="small" type="warning">{{ experiment.difficulty }}</el-tag>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import { VueFlow} from '@vue-flow/core'
import { Background} from  '@vue-flow/background'
import { Controls} from '@vue-flow/controls'
import { MiniMap} from '@vue-flow/minimap'
import { 
  Document, 
  Cpu, 
  VideoPlay, 
  User,
  Monitor,
  Setting,
  DataLine,
  Connection,
  Platform,
  Tools,
  Loading
} from '@element-plus/icons-vue'

const router = useRouter()

// 响应式数据
const showNodeDetail = ref(false)
const selectedNode = ref(null)
const activeTab = ref('lectures')
const mapContainer = ref(null)

// VueFlow 数据
const flowNodes = ref([])
const flowEdges = ref([])
const isLoading = ref(true)

// 加载知识图谱数据
const loadKnowledgeGraph = async () => {
  try {
    const response = await fetch('/src/data/knowledge-graph.json')
    const data = await response.json()
    flowNodes.value = data.nodes
    flowEdges.value = data.edges
    isLoading.value = false
  } catch (error) {
    console.error('加载知识图谱数据失败:', error)
    isLoading.value = false
  }
}

// 保存知识图谱数据
const saveKnowledgeGraph = async () => {
  try {
    const data = {
      nodes: flowNodes.value,
      edges: flowEdges.value
    }
    // 在实际应用中，这里需要调用后端API来保存数据
    // 目前只在控制台输出，表示保存操作
    console.log('保存知识图谱数据:', data)
    localStorage.setItem('knowledge-graph', JSON.stringify(data))
  } catch (error) {
    console.error('保存知识图谱数据失败:', error)
  }
}

// 监听节点和边的变化，自动保存
watch([flowNodes, flowEdges], () => {
  if (!isLoading.value) {
    saveKnowledgeGraph()
  }
}, { deep: true })



// 方法
const handleNodeClick = (event) => {
  const nodeData = event.node.data
  selectedNode.value = {
    title: nodeData.title,
    icon: nodeData.icon,
    description: nodeData.description,
    lectures: nodeData.lectures,
    videos: nodeData.videos,
    experiments: nodeData.experiments
  }
  showNodeDetail.value = true
  activeTab.value = 'lectures'
}



const goToLecture = (lectureId) => {
  router.push(`/lectures/${lectureId}`)
  showNodeDetail.value = false
}

const goToVideo = (videoId) => {
  router.push(`/videos/${videoId}`)
  showNodeDetail.value = false
}

const goToExperiment = (experimentId) => {
  router.push(`/experiments/${experimentId}`)
  showNodeDetail.value = false
}

onMounted(() => {
  loadKnowledgeGraph()
})
</script>

<style scoped>
.knowledge-map-section {
  padding: 10px 0;
  background: white;
}

.section-header {
  text-align: center;
  margin-bottom: 10px;
}

.section-header h2 {
  font-size: 2rem;
  color: #2c3e50;
  margin-bottom: 10px;
}

.section-header p {
  color: #7f8c8d;
  font-size: 1.1rem;
}

.knowledge-map {
  /* width: calc(100vw - 40px); */
  height: calc(100vh - 300px);
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  border-radius: 12px;
  position: relative;
  overflow: hidden;
  margin: 0 auto;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
}

.vue-flow {
  width: 90%;
  height: 100%;
  border-radius: 12px;
}

.knowledge-node {
  background: white;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
  min-width: 120px;
  text-align: center;
  border: 2px solid transparent;
}

.knowledge-node.basic {
  border-color: #67c23a;
  background: linear-gradient(135deg, #ffffff 0%, #f0f9ff 100%);
}

.knowledge-node.intermediate {
  border-color: #e6a23c;
  background: linear-gradient(135deg, #ffffff 0%, #fffbf0 100%);
}

.knowledge-node.advanced {
  border-color: #f56c6c;
  background: linear-gradient(135deg, #ffffff 0%, #fff5f5 100%);
}

.node-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.node-icon {
  color: #409eff;
}

.node-title {
  font-weight: 600;
  color: #303133;
  font-size: 13px;
  line-height: 1.2;
}

.node-subtitle {
  color: #909399;
  font-size: 11px;
  line-height: 1.2;
}

/* VueFlow 样式覆盖 */
.vue-flow__node {
  border: none !important;
}

.vue-flow__edge-path {
  stroke: #409eff;
  stroke-width: 2;
}

.vue-flow__edge:hover .vue-flow__edge-path {
  stroke: #67c23a;
  stroke-width: 3;
}

.vue-flow__controls {
  bottom: 20px;
  left: 20px;
}

.vue-flow__minimap {
  bottom: 20px;
  right: 20px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #409eff;
}

.loading-icon {
  animation: rotate 2s linear infinite;
  margin-bottom: 16px;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.knowledge-node:hover {
  transform: scale(1.05);
  z-index: 20;
}

.knowledge-node:hover .node-content {
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
}

.stats-section {
  padding: 10px 0;
  background: #f8f9fa;
}

.stat-card {
  background: white;
  border-radius: 6px;
  padding: 15px 10px;
  text-align: center;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-5px);
}

.stat-icon {
  margin-bottom: 8px;
}

.stat-number {
  font-size: 1.25rem;
  font-weight: 700;
  color: #2c3e50;
  margin-bottom: 3px;
}

.stat-label {
  color: #7f8c8d;
  font-size: 0.5rem;
}

.node-detail {
  padding: 20px 0;
}

.detail-header {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
}

.detail-icon {
  margin-right: 20px;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 50%;
}

.detail-info h3 {
  margin: 0 0 10px 0;
  color: #2c3e50;
}

.detail-info p {
  margin: 0;
  color: #7f8c8d;
  line-height: 1.6;
}

.content-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.content-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.content-item:hover {
  background: #e3f2fd;
  transform: translateX(5px);
}

.content-item span {
  flex: 1;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .knowledge-map {
    padding: 20px;
    min-height: 400px;
  }
  
  .map-container {
    height: 350px;
  }
  
  .knowledge-node {
    width: 100px;
    height: 90px;
  }
  
  .node-content {
    padding: 10px;
  }
  
  .node-title {
    font-size: 12px;
  }
  
  .node-subtitle {
    font-size: 10px;
  }
  
  .stat-card {
    padding: 20px 15px;
  }
  
  .stat-number {
    font-size: 2rem;
  }
}
</style>